// pages/feedback/feedback.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedbackAction: false,
    informationAction: false,
    // 反馈
    feedback: "",
    // 联系方式
    information: ""
  },

  // 返回
  onNavBackTap() {
    wx.navigateBack({});
  },

  // input聚焦
  onTabAction(e) {
    // console.log(e);
    const type = e.type
    const id = e.currentTarget.id
    if (type === 'focus') {
      if (id == "information") {
        this.setData({ informationAction: true })
      }
      if (id === "feedback") {
        this.setData({ feedbackAction: true })
      }
    }
    if (type === 'blur') {
      if (id == "information") {
        this.setData({ informationAction: false })
      }
      if (id === "feedback") {
        this.setData({ feedbackAction: false })
      }
    }
  },

  // 取消按钮
  cancel() {
    this.setData({
      feedback: "",
      information: ""
    })
  },

  // 提交按钮
  async affirm() {
    const feedback = this.data.feedback
    const information = this.data.information
    if (!feedback) {
      wx.showToast({
        title: "请填写反馈",
        icon: "error"
      })
      return
    }
    if (!information) {
      wx.showToast({
        title: "请填写联系方式",
        icon: "error"
      })
      return
    }

    if (feedback && information) {
      wx.showLoading({ title: '发送意见中...' })
      let result = await wx.cloud.callFunction({
        name: "submitFeedback",
        data: {
          feedback: {
            feedback: this.data.feedback,
            information: this.data.information
          }
        }
      })
      if (result.result.code === 20000) {
        wx.showToast({
          title: "感谢您的意见！",
          icon: "success"
        })
        this.setData({
          feedback: "",
          information: ""
        })
      } else {
        wx.showToast({
          title: "提交失败！",
          icon: "error"
        })
      }
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

})